package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.AdChannel;
import com.heima.model.wemedia.dtos.ChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.service.WmChannelService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {

    private static final short PAGE_MAX_SIZE=100;
    /**
     * 获取频道列表
     *
     * @return
     */
    @Override
    public ResponseResult channels() {
        LambdaQueryWrapper<WmChannel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmChannel::getStatus, 1);
        return ResponseResult.okResult(list(wrapper));
    }

    @Autowired
    private WmChannelMapper wmChannelMapper;

    @Override
    public ResponseResult deleteChannel(Integer id) {
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        return wmChannelMapper.deleteById(id) > 0 ? ResponseResult.okResult(200, "删除成功") : ResponseResult.errorResult(400, "删除失败");
    }

    //分页模糊查询
    @Override
    public ResponseResult channelsList(ChannelDto dto) {
        //检查参数
        if (dto==null){
            //返回参数
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        dto.checkParam();

        //分页
        IPage page = new Page<>(dto.getPage(), dto.getSize());

        LambdaQueryWrapper<WmChannel> queryWrapper = new LambdaQueryWrapper<>();

        //名字模糊查询
        if (StringUtils.isNoneBlank(dto.getName())){
            queryWrapper.like(WmChannel::getName,dto.getName());
        }

        //按照权重排序
        queryWrapper.orderByAsc(WmChannel::getOrd);

        page = page(page,queryWrapper);

        //返回结果
        PageResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }

    //添加
    @Override
    public ResponseResult save(AdChannel dto) {
        //检查数据
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //验证频道是否重复
        LambdaQueryWrapper<WmChannel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmChannel::getName,dto.getName());
        WmChannel wm = getOne(queryWrapper);
        if (wm!=null){
            return ResponseResult.errorResult(400,"该频道存在");
        }
        WmChannel wmChannel = new WmChannel();
        BeanUtils.copyProperties(dto,wmChannel);
        wmChannel.setCreatedTime(new Date());
        wmChannel.setIsDefault(true);
        return save(wmChannel)?ResponseResult.okResult(200,"保存成功"):ResponseResult.errorResult(400,"保存失败");
    }

    //更新数据
    @Override
    public ResponseResult update(AdChannel adChannel) {
        //检查
        if (adChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmChannel wmChannel = new WmChannel();
        BeanUtils.copyProperties(adChannel,wmChannel);
        return updateById(wmChannel)?ResponseResult.okResult(200,"修改成功"):ResponseResult.errorResult(400,"修改失败");
    }
}
